<template>
  <div>
    <!-- 产品内容展示 -->
    <van-dropdown-menu>
      <van-dropdown-item disabled title="综合" />
      <van-dropdown-item
        title="价格"
        @change="priceChange"
        v-model="priceVal"
        :options="option2"
      />
      <van-dropdown-item
        title="分类"
        @change="categoryChange"
        v-model="categoryVal"
        :options="filterCategory"
      />
    </van-dropdown-menu>

    <!-- 搜索提示 -->
    <van-empty v-if="goodsList.length==0" image="search" description="没有您搜索的商品" />
    <Products v-else :goodsList="goodsList" />
  </div>
</template>

<script>
import Products from "./Products.vue";
export default {
  data() {
    return {
      priceVal: "desc",
      categoryVal: 0, // 这个的值是哪一项的value，就表示这一项被勾上

      option2: [
        { text: "价格由高到低", value: "desc" },
        { text: "价格由低到高", value: "asc" },
      ],
    };
  },

  beforeUpdate() {
    // 数据挂载完毕，遍历filterCategory数组，看看哪一项的checked为true
    // 遍历的时候，遇到checked为true时，就把this.categoryVal 改成它的value值，这样这一项就被勾选上了
    this.filterCategory.map((item) => {
      // 如果item的checked为true，就改this.categoryVal
      if (item.checked) {
        // console.log(item);
        this.categoryVal = item.value;
        return;
      }
    });
  },
  props: ["filterCategory", "goodsList"],
  components: {
    Products,
  },
  methods: {
    categoryChange(value) {
      // console.log(value); // 就是分类id的值
      this.$emit("categoryChange", value);
    },
    priceChange(value) {
      // console.log(value); // 就是排序order的值
      this.$emit("priceChange", value);
    },
  },
};
</script>

<style lang="less" scoped></style>
